import java.util.ArrayList;

import processing.core.*;


public class MapCluster {
	public int count;
	private float centerX = 0;
	private float centerY = 0;
	public float avg_Latitude = 0;
	public float avg_Longitude = 0;
	public ArrayList<Case> Cases;
	public ArrayList<Float> pIntervals;
	public float radius;
	public float defaultR = 40;
	public float percent = 0;
	public float highPercent = 100;
	int scaleFactor = 1;
	// public image to draw 
	public int color = 0; // how many fatalaties represented in shades of color
	private PApplet parent;
	public State whichState;
	public boolean colorsOn = true;
	public MapCluster(PApplet parent,int scaleFactor, State state, float percent, float percentHigh){
		this.parent = parent;
		Cases = new ArrayList<Case>();
		pIntervals = new ArrayList<Float>();
		radius = defaultR*scaleFactor;
		this.percent = percent;
		this.highPercent = percentHigh;
		setMaxPercent(percentHigh);
		whichState = state;
	}
	
	
	public MapCluster(MapCluster copy){
		this.parent = copy.parent;
		Cases = new ArrayList<Case>(copy.Cases);
		pIntervals = new ArrayList<Float>(copy.pIntervals);
		radius = copy.radius;
		this.percent = copy.percent;
		this.highPercent = copy.highPercent;
		this.color = copy.color;
		this.whichState = copy.whichState;
		
	}
	
        public void setColor(int color){
          this.color = color;
        }
	
	public MapCluster(PApplet parent,int scaleFactor, State state){
		this.parent = parent;
		Cases = new ArrayList<Case>();
		pIntervals = new ArrayList<Float>();
		radius = defaultR*scaleFactor;
		this.percent = percent;
		whichState = state;
	}
	
	
	public void insertCase(Case new_case){
		float sumLat = 0;
		float sumLong = 0;
		Cases.add(new_case);
		for(Case c : Cases){
			sumLat += c.latitude;
			sumLong += c.longitude;
		}
		avg_Latitude = (float)(sumLat/Cases.size());
		avg_Longitude = (float)(sumLong/Cases.size());
	}
	public void insertCases(ArrayList<Case> cases){
		float sumLat = 0;
		float sumLong = 0;
		Cases.addAll(cases);
		for(Case c : Cases){
			sumLat += c.latitude;
			sumLong += c.longitude;
		}
		avg_Latitude = (float)(sumLat/Cases.size());
		avg_Longitude = (float)(sumLong/Cases.size());
	}
	public void clearCases(){
		Cases.clear();
		avg_Latitude = 0;
		avg_Longitude = 0;
	}
	public void setCountThreshold(int count){
		this.count = count;
	}
	
	public void zoomLevelChange(int zoomLevel){
		// adjust the radius here.
		if(zoomLevel == 0){
			radius = 10*scaleFactor;
		}
		else if(zoomLevel == 1){
			radius = 12*scaleFactor;
		}
		else if(zoomLevel == 2){
			radius = 12*scaleFactor;
		}
		else if(zoomLevel == 3){
			radius = 14*scaleFactor;
		}
		else if(zoomLevel == 4){
			radius = 20*scaleFactor;
		}
		else if(zoomLevel == 5){
			radius = 25*scaleFactor;
		}
		else if(zoomLevel == 6){
			radius = 40*scaleFactor;
		}
		else if(zoomLevel == 7){
			radius = 50*scaleFactor;
		}
		else if(zoomLevel == 8){
			radius = 28*scaleFactor;
		}
		else if(zoomLevel == 9){
			radius = 30*scaleFactor;
		}
		else if(zoomLevel == 10){
			radius = 60*scaleFactor;
		}
		else if(zoomLevel == 11){
			radius = 100*scaleFactor;
		}
		else if(zoomLevel == 12){
			radius = 13*scaleFactor;
		}
		else if(zoomLevel >= 13){
			radius = 17*scaleFactor;
		}
		
	}
	
	public void setMaxPercent(float percent){
		highPercent = percent;
		pIntervals.clear();
		for(int i = 4; i > 0; i--){
			float inter = parent.lerp(0, highPercent, (float)((float)highPercent/(float)i));
			pIntervals.add(inter);
		}
	}
	
	public void draw(float centerX, float centerY){
		this.centerX = centerX;
		this.centerY = centerY;
		if(colorsOn){
			for(Float f : pIntervals){
				if(percent < pIntervals.get(0) && percent > 0){
					color = parent.color(0, 102, 55); 
					break;
				}
				else if(percent > pIntervals.get(0) && percent < pIntervals.get(1)){
					color = parent.color(235, 243, 53);
					break;
				}
				else if(percent > pIntervals.get(1) && percent < pIntervals.get(2)){
					color = parent.color(235, 104, 53);
					break;
				}
				else if(percent > pIntervals.get(2)){
					color = parent.color(235, 18, 53);
					break;
				}
				else{
					color = parent.color(0, 0, 0);
				}
			}
		}
		else{
			color = parent.color(0,0,0);
		}		
		parent.fill(color,90);
		parent.noStroke();
		parent.ellipseMode(parent.RADIUS);
		parent.ellipse(this.centerX, this.centerY, radius, radius);
	}
	public boolean touchHandler(float xPos, float yPos){
		// if the radius distance is less than the radius active the touch
		double radiusDist = Math.sqrt(Math.pow(centerX-xPos, 2)+Math.pow(centerY-yPos, 2));
		//return radiusDist <= radius;
		return centerX-radius < xPos && centerX+radius > xPos && centerY-radius < yPos && centerY+radius > yPos && radiusDist < 20; 
	}
}
